package com.demo.imageswitcherdemo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private int imgIds[] = {R.drawable.bomb5, R.drawable.bomb7, R.drawable.bomb8,
            R.drawable.bomb9, R.drawable.bomb10, R.drawable.bomb11, R.drawable.bomb12, R.drawable.bomb13,
            R.drawable.bomb14, R.drawable.bomb15, R.drawable.bomb16};
    private ImageSwitcher imgSwitch = null;
    private GridView gridView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgSwitch = (ImageSwitcher) findViewById(R.id.img_switch);
        imgSwitch.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
//                ImageView imgView=new ImageView(MainActivity.this);
////                imgView.setLayoutParams(new ViewGroup.LayoutParams(400,400));
//                imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//                return imgView;
                ImageView imgView = (ImageView) LayoutInflater.from(MainActivity.this).inflate(R.layout.factory, null, false);
                return imgView;
            }
        });
        imgSwitch.setInAnimation(MainActivity.this, android.R.anim.fade_in);
        imgSwitch.setOutAnimation(MainActivity.this, android.R.anim.fade_out);

        gridView = (GridView) findViewById(R.id.grid);

        BaseAdapter ba = new BaseAdapter() {
            @Override
            public int getCount() {
                return imgIds.length;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView imgView = new ImageView(MainActivity.this);
                imgView.setLayoutParams(new ViewGroup.LayoutParams(150, 150));
                imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imgView.setImageResource(imgIds[position]);
                return imgView;
            }
        };
        gridView.setAdapter(ba);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                imgSwitch.setImageResource(imgIds[position]);
            }
        });
    }
}
